package com.beasts.antpage.core.utils.file;

import com.beasts.antpage.core.utils.unique.UUIDUtil;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

/**
 * 文件上传
 *
 * @author Locker
 * @date 11/07/2018 7:05 PM
 * @since 1.0
 */
public class UploadUtil {

    // TODO 文件上传

    /**
     * 单文件上传
     *
     * @param req
     * @return
     */
    public static String saveFile(HttpServletRequest req) throws IOException {

        String qiniuPath = "";

        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) req;
        // 原始文件名
//        String originalFileNameLeft = "";
        // 新文件名
        String newPicName = "";
        // 扩展名
        String expandedName = "";
        // 目录分隔符
        String separator = File.separator;
        // 获取项目路径
        String uploadPath = System.getProperty("user.dir").replace("bin", " webapps") + File.separator;
//        String path = req.getSession().getServletContext().getRealPath("/") + uploadPath;
//        path = path.substring(0, path.indexOf("")) + "\\" + uploadPath;
        // 自定义路径
        String directory = uploadPath + "wyInFile" + separator;
        // 遍历文件
        for (Iterator it = multipartHttpServletRequest.getFileNames(); it.hasNext(); ) {
            String key = (String) it.next();
            MultipartFile multipartFile = multipartHttpServletRequest.getFile(key);
            // TODO 设置文件最大尺寸，1024*1024*2 为 2M
//            if (multipartFile.getSize() > 1024 * 1024 * 2) {
//                return "";
//            }

            if (multipartFile.getSize() != 0) {
                // 原始文件名
//                originalFileNameLeft = multipartFile.getOriginalFilename();
                // 匹配扩展名
                expandedName = fileExtension(multipartFile);
                // 新的图片名称
//                newPicName = UUIDUtil.getUUID22() + originalFileNameLeft.substring(originalFileNameLeft.lastIndexOf("."));
                newPicName = UUIDUtil.getUUID22() + expandedName;
                // 新图片，写入磁盘
//                File f = new File(directory, newPicName);
//                if (!f.exists()) {
//                    // 先创建文件所在的目录
//                    f.getParentFile().mkdirs();
//                }
//                multipartFile.transferTo(f);
            }

            // 七牛上传
            FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
            qiniuPath = new QiniuUtil().uploadImg(inputStream, newPicName);

        }

//        return directory + newPicName;
        return qiniuPath;
    }

    /**
     * 获取文件扩展名
     *
     * @param multipartFile
     * @return
     */
    public static String fileExtension(MultipartFile multipartFile) {

        String expandedName;

        // 方法一：文件内容类型解析
//        // 上传内容类型
//        String uploadContentType = multipartFile.getContentType();
//        // 匹配扩展名
//        switch (uploadContentType) {
//            // IE6上传jpg图片的headimageContentType是image/pjpeg，而IE9以及火狐上传的jpg图片是image/jpeg
//            case "image/jpeg":
//                expandedName = ".jpg";
//                break;
//            case "image/pjpeg":
//                expandedName = ".jpg";
//                break;
//            // IE6上传的png图片的headimageContentType是"image/x-png"
//            case "image/png":
//                expandedName = ".png";
//                break;
//            case "image/x-png":
//                expandedName = ".png";
//                break;
//            case "image/gif":
//                expandedName = ".gif";
//                break;
//            case "image/bmp":
//                expandedName = ".bmp";
//                break;
//            default:
//                expandedName = ".jpg";
//        }

        // 方法二：原始文件名解析
        String originalFileNameLeft = multipartFile.getOriginalFilename();
        expandedName = originalFileNameLeft.substring(originalFileNameLeft.lastIndexOf("."));

        return expandedName;
    }

    // ---------------

    /**
     * webmvc 上传图片
     */
    public String uploadImg(HttpServletRequest req) throws IOException {

        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) req;
        // 原始文件名
        String originalFileNameLeft = "";
        // 新文件名
        String newPicName = "";
        // 扩展名
        String expandedName = "";
        // 目录分隔符
        String separator = File.separator;
        String directory = "" + separator;
        // 遍历文件
        for (Iterator it = multipartHttpServletRequest.getFileNames(); it.hasNext(); ) {
            String key = (String) it.next();
            MultipartFile multipartFile = multipartHttpServletRequest.getFile(key);

            if (multipartFile.getSize() != 0) {
                // 原始文件名
                originalFileNameLeft = multipartFile.getOriginalFilename();
                // 匹配扩展名
//                expandedName = fileExtension(multipartFile);
                // 新的图片名称
                newPicName = UUIDUtil.getUUID22() + "__" + originalFileNameLeft;
                // 新图片，写入磁盘
                File f = new File(directory, newPicName);
                if (!f.exists()) {
                    // 先创建文件所在的目录
                    f.getParentFile().mkdirs();
                }
                multipartFile.transferTo(f);
            }
        }

        return directory + newPicName;
    }


}
